﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace UpdateForm
{
    public partial class UpdateForm : Form
    {
        public UpdateForm()
        {
            InitializeComponent();
        }
        private string processPath = System.Environment.CurrentDirectory;

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                textBox3.Text = "开始更新\r\n";
                if (textBox1.Text == "")
                {
                    textBox3.Text = "网站名称不能为空\r\n";
                    return;
                }
                var webManager = new Microsoft.Web.Administration.ServerManager();
                var startSite = webManager.Sites[textBox1.Text];
                if (startSite == null)
                {
                    textBox3.Text = "未找到指定的网站:" + textBox1.Text + "\r\n";
                    return;
                }

                if (textBox2.Text == "")
                {
                    textBox3.Text = "物理路径不能为空\r\n";
                    return;
                }
                if (!Directory.Exists(textBox2.Text))
                {
                    textBox3.Text = "物理路径不存在\r\n";
                    return;
                }

                foreach (var site in webManager.Sites)
                {
                    if (site.Name == textBox1.Text)
                        site.Stop();
                    textBox3.Text = "网站已停止\r\n";
                }
                textBox3.Text = "开始复制文件\r\n";
                DirectoryInfo root = new DirectoryInfo(processPath + "/UpdateFile");
                foreach (FileInfo f in root.GetFiles())
                {
                    File.Move(f.FullName, textBox2.Text + "/" + f.Name);
                    textBox3.Text = f.FullName + "文件复制成功\r\n";
                }
                startSite.Start();
                textBox3.Text = textBox1.Text+ "网站启动成功\r\n";
            }
            catch (Exception ex)
            {
                MessageBox.Show("更新失败"+ex.Message);
            }

        }

        private void comboBox1_MouseEnter(object sender, EventArgs e)
        {
            if (textBox4.Text == "")
            {
                MessageBox.Show("服务器地址不能为空！");
                return;
            }
            if (textBox5.Text == "")
            {
                MessageBox.Show("用户名不能为空！");
                return;
            }
            string sqlcon = "Data Source=" + textBox4.Text + ";Initial Catalog=Master;User ID=" + textBox5.Text + ";Password=" + textBox6.Text + "";
            //DataSet database = SQLDataBase.GetDataBase(sqlcon);
            DataSet database = DataBase.GetDataBase(sqlcon);
            if (database == null)
            {
                MessageBox.Show("连接数据库失败！");
            }
            comboBox1.DataSource = database.Tables[0].DefaultView;
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "Name";
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sqlfile = processPath + "/更新.sql";
            if (File.Exists(sqlfile))
            {
                MessageBox.Show("没有需要更新的sql语句");
                return;
            }
            string file = "";
            StreamReader sr = new StreamReader(sqlfile);

            while (!sr.EndOfStream)
            {
                file += sr.ReadToEnd();
            }
            sr.Close();
            string contring = "Data Source=" + textBox4.Text + ";Initial Catalog="+comboBox1.SelectedValue+";User ID=" + textBox5.Text + ";Password=" + textBox6.Text + "";
            string ver = DataBase.ExecSql(contring,file);
            if (ver == "")
            {
                MessageBox.Show("更新成功！");
            }
            else
            {
                MessageBox.Show("更新失败"+ver);
            }
        }

        private void UpdateForm_Load(object sender, EventArgs e)
        {
            string str = "0960000601";
            string s = str.Substring(3,str.Length-3);
        }
    }
}
